$(function() {

    var projectId = $('#project_id_input').val();

    uploadData();
    uploadLogo();
    uploadMusic();
    uploadImage();

    function showNote(text) {

        $('#cover').css("display", "block");
        $('#note_container').css("display", "block");
        $('#note_container').text(text);
    }

    function uploadData() {

        var uploader = new plupload.Uploader({
            browse_button : 'upload_data_btn',
            url : '/item/addData',
            multipart_params: {
                projectId : projectId
            },
            filters: {
                mime_types : [
                    {
                        title : "data files",
                        extensions : "data"
                    }
                ],
                max_file_size : '10MB',
                prevent_duplicates : true
            },
            multi_selection	: false
        });
        uploader.init();
        uploader.bind('FilesAdded',function(uploader, files) {
            uploader.start();
        });
        uploader.bind('UploadProgress',function(uploader, file) {
            showNote('正在上传配置文件...' + file.percent + '%');
        });
        uploader.bind('FileUploaded',function(uploader, file, info) {
            var status = info.status;
            if(status != 200) {
                alert('配置文件上传失败！');
                return;
            }
            var response = info.response;
            if(!response) {
                alert('配置文件上传失败！');
                return;
            }

            var responseObj = null;
            var retCode = null;
            try {
                responseObj = JSON.parse(response);
                retCode = responseObj.retCode;
            } catch (e) {
                alert('配置文件上传失败！');
                return;
            }
            if(retCode == 0) {
                var errCode = responseObj.result.errCode;
                alert('配置文件上传失败！');
                switch(errCode) {
                    case -1:
                        alert('账号或密码错误！');
                        break;
                    case -2:
                        alert('账号不存在！');
                        break;
                }
            } else if(retCode == 1) {

                showNote('配置文件上传成功！');
                setTimeout(function () {
                    location.reload(true);
                }, 2000);
            }
        });
        uploader.bind('UploadComplete',function() {

        });
        uploader.bind('Error',function(uploader, err, info) {
            alert('文件上传失败！');
            location.reload(true);
        });
    }

    function uploadLogo() {
        var uploader = new plupload.Uploader({
            browse_button : 'upload_logo_btn',
            url : '/item/addLogo',
            multipart_params: {
                projectId : projectId
            },
            filters: {
                mime_types : [
                    {
                        title : "logo files",
                        extensions : "jpg,png,jpeg"
                    }
                ],
                max_file_size : '5MB',
                prevent_duplicates : true
            },
            multi_selection	: false
        });
        uploader.init();
        uploader.bind('FilesAdded',function(uploader, files) {
            uploader.start();
        });
        uploader.bind('UploadProgress',function(uploader, file) {

            showNote('正在上传Logo...' + file.percent + '%');
        });
        uploader.bind('FileUploaded',function(uploader, file, info) {
            var status = info.status;
            if(status != 200) {
                alert('Logo上传失败！');
                return;
            }
            var response = info.response;
            if(!response) {
                alert('Logo上传失败！');
                return;
            }

            var responseObj = null;
            var retCode = null;
            try {
                responseObj = JSON.parse(response);
                retCode = responseObj.retCode;
            } catch (e) {
                alert('Logo上传失败！');
                return;
            }
            if(retCode == 0) {
                var errCode = responseObj.result.errCode;
                alert('Logo上传失败！');
                switch(errCode) {
                    case -1:
                        alert('账号或密码错误！');
                        break;
                    case -2:
                        alert('账号不存在！');
                        break;
                }
            } else if(retCode == 1) {
                showNote('Logo上传成功！');
                setTimeout(function () {
                    location.reload(true);
                }, 2000);
            }
        });
        uploader.bind('Error',function(uploader, err, info) {
            alert('Logo上传失败！');
            location.reload(true);
        });
    }

    function uploadMusic() {

        var uploader = new plupload.Uploader({
            browse_button : 'upload_music_btn',
            url : '/item/addMusic',
            multipart_params: {
                projectId : projectId
            },
            filters: {
                mime_types : [
                    {
                        title : "music files",
                        extensions : "mp3"
                    }
                ],
                max_file_size : '10MB',
                prevent_duplicates : true
            },
            multi_selection	: false
        });
        uploader.init();
        uploader.bind('FilesAdded',function(uploader, files) {
            uploader.start();
        });
        uploader.bind('UploadProgress',function(uploader, file) {
            showNote('正在上传音乐...' + file.percent + '%');
            if(file.percent >= 100) {
                // window.location.href = '/project/detail/projectId/' + projectId;
                // alert('file upload complete');
                //             location.reload(true);
            }
            // $('#file-'+file.id+' .progress').css('width', file.percent + '%');//控制进度条
        });
        uploader.bind('FileUploaded',function(uploader, file, info) {
            var status = info.status;
            console.log('FileUploaded()<<<---status = ' + status);
            if(status != 200) {
                alert('音乐上传失败！');
                return;
            }
            var response = info.response;
            console.log('FileUploaded()<<<---response = ' + response);
            if(!response) {
                alert('音乐上传失败！');
                return;
            }

            var responseObj = null;
            var retCode = null;
            try {
                responseObj = JSON.parse(response);
                retCode = responseObj.retCode;
            } catch (e) {
                alert('音乐上传失败！' + e.name + ": " + e.message);
                return;
            }
            if(retCode == 0) {
                var errCode = responseObj.result.errCode;
                alert('音乐上传失败！');
                switch(errCode) {
                    case -1:
                        alert('账号或密码错误！');
                        console.log('submit()<<<---retCode != 1');
                        break;
                    case -2:
                        alert('账号不存在！');
                        console.log('submit()<<<---retCode != 1');
                        break;
                }
            } else if(retCode == 1) {
                showNote('音乐上传成功！');
                setTimeout(function () {
                    location.reload(true);
                }, 2000);
            }
        });
        uploader.bind('Error',function(uploader, err, info) {
            alert('音乐上传失败！' + err + info);
            // location.reload(true);
        });
    }

    function uploadImage() {

        var fileNum = 0;
        var token = $('#token_input').val();
        if(!token) {
            alert('无权上传图片！');
            return;
        }
        var domain = $('#domain_input').val();
        if(!domain) {
            alert('无权上传图片！');
            return;
        }

        var uploader = Qiniu.uploader({
            browse_button: 'upload_image_btn',
            multi_selection: true,
            uptoken : token,
            unique_names: true,              // 默认false，key为文件名。若开启该选项，JS-SDK会为每个文件自动生成key（文件名）
            domain: domain,
            get_new_uptoken: false,
            x_vars: {
                'fSize' : function(up, file) {
                    return file.size;
                }
            },
            filters: {
                mime_types : [
                    {
                        title : 'image files',
                        extensions : 'jpg'
                    }
                ],
                max_file_size: '10MB',
                prevent_duplicates : true
            },
            init: {
                'FilesAdded': function(up, files) {
                    fileNum = files.length;
                    if(fileNum > 0) {
                        uploader.start();
                    }
                },
                'UploadProgress': function(up, file) {

                    var uploaded = file.percent + (fileNum - up.total.queued) * 100;
                    var total = fileNum * 100;
                    var progress = Math.floor(uploaded / total * 100);
                    showNote('共（' + fileNum +'）张图片正在上传...' + progress + '%');
                    // $('#upload_image_btn').html('共' + 84 +'个文件正在上传（' + progress + '%）');
                },
                'FileUploaded': function(up, file, info) {
                    console.log('FileUploaded()<<<---info = ' + info);
                    if(!info) {
                        alert('文件上传失败');
                    }
                    var infoObj = JSON.parse(info);
                    if(!infoObj) {
                        alert('文件上传失败');
                    }
                    console.log('FileUploaded()<<<---infoObj.retCode = ' + infoObj.retCode);
                    if(infoObj.retCode != 1) {
                        alert('文件上传失败');
                    }
                },
                'UploadComplete': function() {
                    showNote('图片上传成功！');
                    setTimeout(function () {
                        location.reload(true);
                    }, 2000);
                },
                'Error': function(up, err, errTip) {
                    // alert(errTip);
                    console.log('Error()<<<---errTip = ' + errTip);
                    // location.reload(true);
                }
            }
        });
    }
    


});